package com.zx.web.servelt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zx.bean.Address;
import com.zx.bean.Cart;
import com.zx.bean.Order;
import com.zx.bean.User;
import com.zx.web.service.AddressService;
import com.zx.web.service.CartService;
import com.zx.web.service.OrderService;
import com.zx.web.service.impl.AddressServiceImpl;
import com.zx.web.service.impl.CartServiceImpl;
import com.zx.web.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.servelt
 * @Description:
 * @Date 2022/9/15 10:25
 */
@WebServlet("/orderservlet")
public class Orderservlet extends BaseServlet {
    CartService service = new CartServiceImpl();
    OrderService orderService = new OrderServiceImpl();
    /**
     *订单预览
     * @param req
     * @param resp
     */
    public String getOrderView(HttpServletRequest req, HttpServletResponse resp){
        //获取登录者userId
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();

        List<Cart> carts = service.findCartByUserId(userId);
        req.setAttribute("carts",carts);

        AddressService addressService = new AddressServiceImpl();
        List<Address> addList = addressService.getAddressByUid(userId);
        req.setAttribute("addList",addList);

        return "forward:/order.jsp";
    }

    /**
     * 提交订单
     * @param req
     * @param resp
     */
    public String addOrder(HttpServletRequest req, HttpServletResponse resp){

        //地址id
        int aid = Integer.parseInt(req.getParameter("aid"));
        //获取用户id
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();

        Order order = orderService.addOrder(aid,userId);

        req.setAttribute("order",order);
        return "forward:/orderSuccess.jsp";

    }

    /**
     * 支付成功
     * @param req
     * @param resp
     * @return
     */
    public String paySuccess(HttpServletRequest req,HttpServletResponse resp){

        String result = req.getParameter("result");

        System.out.println(result);

        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject result1 = jsonObject.getJSONObject("result");
        System.out.println(result1);

        String result_code = result1.getString("result_code");

        //获取订单编号
        String id = result1.getString("out_trade_no");
        if("SUCCESS".equalsIgnoreCase(result_code)){
            orderService.updateStatus(id);
        }
        //获取登录者userId
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();
        List<Order> orderList = orderService.findOrderById(userId);
        req.setAttribute("orderList",orderList);
        return "forward:/orderList.jsp";
    }

    /**
     *我的订单
     * @param req
     * @param resp
     * @return
     */
    public String getOrderList(HttpServletRequest req,HttpServletResponse resp){
        //获取登录者userId
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();
        List<Order> orderList = orderService.findOrderById(userId);
        req.setAttribute("orderList",orderList);
        return "forward:/orderList.jsp";
    }
}
